package au.com.iglootech.winerymap.ui.view.winerymanage;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.component.IGTTable;
import au.com.iglootech.core.IGTIView;
import au.com.iglootech.core.IGTIViewPresenter;
import au.com.iglootech.winerymap.entity.WineryInfo;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * 
 * @author Nicholas Zhu
 * 
 */
@Configurable(preConstruction = true)
@Component
@Scope("request")
public class ManageWineryTab extends CustomComponent implements IGTIView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1586094778227100887L;

	private final VerticalLayout mainLayout = new VerticalLayout();
	private BeanContainer<String, WineryInfo> beans;
	private Table companyTable;
	@Autowired
	private IPresenter presenter;

	/**
	 * constructor.
	 */
	public ManageWineryTab() {
		setCaption("Manage Winery");
		initContent();
	}

	/**
	 * 
	 */
	@SuppressWarnings("serial")
	private void initContent() {
		Panel panel = new Panel();
		beans = new BeanContainer<String, WineryInfo>(WineryInfo.class);

		// Use the name property as the item ID of the bean
		List<WineryInfo> wineryList = presenter.getAllWinery();
		beans.setBeanIdProperty("key");
		for (WineryInfo winery : wineryList) {
			beans.addBean(winery);
		}

		companyTable = new IGTTable("Winery List", beans);
		companyTable.setVisibleColumns(new String[] { "companyName", "contactName", "contactNumber", "tfn", "email", "address1", "address2",
				"address3" });
		companyTable.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
		companyTable.setWidth("100%");
		companyTable.setHeight("500px");
		companyTable.setSelectable(true);
		companyTable.setImmediate(true);
		companyTable.addListener(new Property.ValueChangeListener() {
			@Override
			public void valueChange(final ValueChangeEvent event) {

			}
		});
		panel.addComponent(companyTable);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}

	/**
	 * refresh.
	 */
	public void refresh() {
		List<WineryInfo> wineryList = presenter.getAllWinery();
		beans.removeAllItems();
		for (WineryInfo winery : wineryList) {
			beans.addBean(winery);
		}
	}

	/**
	 * 
	 * @author Nicholas Zhu
	 * 
	 */
	public interface IPresenter extends IGTIViewPresenter {
		/**
		 * 
		 * @return list of all wineries
		 */
		List<WineryInfo> getAllWinery();
	}

}
